package com.tz.fruit.dao.impl;

import com.tz.fruit.dao.IProductDao;
import com.tz.fruit.entity.*;
import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class ProductDaoImpl implements IProductDao{
    @Autowired
    SessionFactory sessionFactory;

    public Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    @Override
    public void save(Product p) {
        getSession().save(p);
    }

    @Override
    public List<Product> findAll(String keyWord, RecState recState, Category category,String loc,boolean order,Long pageNow, Long pageSize) {
        Criteria c=getSession().createCriteria(Product.class,"p");
        if (null!=keyWord&&keyWord.trim().length()>0)
            c.add(Restrictions.like("pName","%"+keyWord+"%"));
        if (null!=recState)
            c.add(Restrictions.eq("recState",recState));
        if (null!=category)
            c.add(Restrictions.eq("category",category));
        if (null!=loc&&loc.trim().length()>0&&(!loc.equals("-1")))
            c.add(Restrictions.eq("origin",loc));
        if (order){
            c.addOrder(org.hibernate.criterion.Order.desc("price"));
        }else{
            c.addOrder(org.hibernate.criterion.Order.asc("price"));
        }
        return c.setFirstResult((int) ((pageNow - 1) * pageSize)).setMaxResults((int) (1 * pageSize)).list();
    }

    @Override
    public Long findRows(String keyWord, RecState recState,Category category,String loc) {
        Criteria c=getSession().createCriteria(Product.class,"p");
        if (null!=keyWord&&keyWord.trim().length()>0)
            c.add(Restrictions.like("pName","%"+keyWord+"%"));
        if (null!=recState)
            c.add(Restrictions.eq("recState",recState));
        if (null!=category)
            c.add(Restrictions.eq("category",category));
        if (null!=loc&&loc.trim().length()>0&&(!loc.equals("-1")))
            c.add(Restrictions.eq("origin",loc));
        c.setProjection(Projections.rowCount());
        return (Long) c.uniqueResult();
    }



    @Override
    public void save(Intro intro) {
        getSession().save(intro);
    }

    @Override
    public void delByIntroId(Long introId) {
        Intro i = (Intro) getSession().get(Intro.class,introId);
        if(null != i){
            getSession().delete(i);
        }
    }

    @Override
    public void update(Intro intro) {
        Intro i = (Intro) getSession().get(Intro.class,intro.getIntroId());
        if(null != i){
            //清空缓存
            getSession().clear();
            //更新商品简介
            getSession().update(intro);
        }
    }

    @Override
    public void update(Product p) {
        Product product = (Product) getSession().get(Product.class,p.getPid());
        if(null != product){
            //清空缓存
            getSession().clear();
            //更新商品
            getSession().update(p);
        }
    }

    @Override
    public Product findById(Long pid) {
        return (Product) getSession().get(Product.class,pid);
    }

    @Override
    public Product findByPNo(String pNo) {
        return (Product) getSession().createQuery("from Product where pNo=?").setParameter(0,pNo).uniqueResult();
    }

    @Override
    public Product findByPName(String pName) {
        return (Product) getSession().createQuery("from Product where pName=?").setParameter(0,pName).uniqueResult();
    }

    @Override
    public void save(Category c) {
        getSession().save(c);
    }

    @Override
    public void delByCategoryId(Long categoryId) {
        Category c = (Category) getSession().get(Category.class,categoryId);
        if(null != c){
            Long count = (Long) getSession().createQuery("select count(*) from Product where category.categoryId=?").setParameter(0,categoryId).uniqueResult();
            if(count==0){
                getSession().delete(c);
            }
        }
    }

    @Override
    public void update(Category c) {
        Category category = (Category) getSession().get(Category.class,c.getCategoryId());
        if(null != category){
            //清空缓存
            getSession().clear();
            //更新商品
            getSession().update(c);
        }
    }

    @Override
    public List<Category> findAllCategory(String categoryNo, String categoryName) {
        Criteria c=getSession().createCriteria(Category.class,"c");
        if (null!=categoryNo&&categoryNo.trim().length()>0)
            c.add(Restrictions.like("c.categoryNo",categoryNo, MatchMode.ANYWHERE));
        if (null!=categoryName&&categoryName.trim().length()>0)
            c.add(Restrictions.like("c.categoryName",categoryName, MatchMode.ANYWHERE));
        return c.list();
    }

    @Override
    public Category findByCategoryId(Long categoryId) {
        return (Category) getSession().createQuery("from Category where categoryId=?").setParameter(0,categoryId).uniqueResult();
    }

    @Override
    public Category findByCategoryNo(String categoryNo) {
        return (Category) getSession().createQuery("from Category where categoryNo=?").setParameter(0,categoryNo).uniqueResult();
    }

    @Override
    public Category findByCategoryName(String categoryName) {
        return (Category) getSession().createQuery("from Category where categoryName=?").setParameter(0,categoryName).uniqueResult();
    }

    @Override
    public void delById(Long pid) {
        Session session = getSession();
        Product p = (Product) session.get(Product.class,pid);
        if(null != p){
            //删除商品简介
            session.createQuery("delete Intro where product.pid=?").setParameter(0,pid).executeUpdate();
            //删除商品图片
            session.createQuery("delete Image where product.pid=?").setParameter(0,pid).executeUpdate();
            //删除商品评论
            session.createQuery("delete Comment where product.pid=?").setParameter(0,pid).executeUpdate();
            //删除商品
            session.delete(p);
        }
    }

    @Override
    public List<Intro> findIntroByPid(Long pid) {
        return getSession().createQuery("from Intro where product.pid=?").setParameter(0,pid).list();
    }

    @Override
    public Intro findByIntroId(Long introId) {
        return (Intro) getSession().get(Intro.class,introId);
    }

    @Override
    public Intro findByKey(String key) {
        return (Intro) getSession().createQuery("from Intro where key=?").setParameter(0,key).uniqueResult();
    }

    @Override
    public void save(Comment comment) {
        getSession().save(comment);
    }

    @Override
    public void delByCommmentId(Long commmentId) {
        Comment c = (Comment) getSession().get(Comment.class,commmentId);
        if(null!=c){
            //删除评论图片
            getSession().createQuery("delete Image where comment.commentId=?").setParameter(0,commmentId).executeUpdate();
            //删除商品评论
            getSession().delete(c);
        }
    }

    @Override
    public void update(Comment comment) {
        Comment c = (Comment) getSession().get(Comment.class,comment.getCommentId());
        if(null != c){
            //清空缓存
            getSession().clear();
            //更新商品
            getSession().update(comment);
        }
    }

    @Override
    public List<Image> findImgByPid(Long pid) {
        return getSession().createQuery("from Image where product.pid=?").setParameter(0,pid).list();
    }

    @Override
    public Image findByImageId(Long imageId) {
        return (Image) getSession().get(Image.class,imageId);
    }

    @Override
    public List<Comment> findCommentByPid(Long pid) {
        return getSession().createQuery("from Comment where product.pid=?").setParameter(0,pid).list();
    }

    @Override
    public Comment findByCommmentId(Long commmentId) {
        return (Comment) getSession().get(Comment.class,commmentId);
    }

    @Override
    public void save(Image image) {
        getSession().save(image);
    }

    @Override
    public void delByImageId(Long imageId) {
        Image i = (Image) getSession().get(Image.class,imageId);
        if(null!=i){
            //删除图片
            getSession().delete(i);
        }
    }

    @Override
    public void update(Image image) {
        Image i = (Image) getSession().get(Image.class,image.getImageId());
        if(null != i){
            //清空缓存
            getSession().clear();
            //更新商品
            getSession().update(image);
        }
    }



    @Override
    public List<Comment> findAllC(Long id, Long pageNow, Long pageSize) {
        Product p= (Product) getSession().get(Product.class,id);
        return getSession().createQuery("from Comment where product=:product order by commentDate desc")
                .setParameter("product",p)
                .setFirstResult((int) ((pageNow - 1) * pageSize))
                .setMaxResults((int) (1 * pageSize)).list();
    }

    @Override
    public Long findRowsC(Long id) {
        Product p= (Product) getSession().get(Product.class,id);
        Criteria c=getSession().createCriteria(Comment.class,"c");
        c.add(Restrictions.eq("product",p));
        c.setProjection(Projections.rowCount());
        return (Long) c.uniqueResult();
    }


    @Override
    public Product findProductById(Long id) {
        return (Product) getSession().get(Product.class,id);
    }

    @Override
    public Long findPidByPNo(String pNo) {
        return (Long) getSession().createQuery("select pid from Product where pNo=:pno").setParameter("pno",pNo).uniqueResult();
    }
}
